package com.jame.util;

import com.jame.aop.AopResult;
import com.jame.aop.ExecuteClassParticulars;
import com.jame.context.ApplicationContext;
import com.jame.typeenum.AccessModifier;
import org.apache.log4j.Logger;


/**
 * @author : Jame
 * @date : 2021-06-16 09:54
 * 工具类
 **/
public class StringUtil {


    private static final Logger logger = Logger.getLogger(ApplicationContext.class);

    public static String getBeanName(String beanName) {
        return beanName.substring(3);
    }

    /**
     * *.*.*.*
     * *.com.jame.*.*.*
     * *.com.jame.Test.*.*
     *
     * @param aopResult
     * @return 解析表达式
     */
    public static ExecuteClassParticulars analysis(AopResult aopResult) {
        String value = aopResult.getValue();
        if (value == null || value.equals("")) {
            logger.error("表达式不能为null或为空");
            return null;
        }
        String qualifier = null;
        String returnType = null;
        String methodName = null;
        try {
            qualifier = value.substring(0, value.indexOf("."));
            returnType = value.substring(value.lastIndexOf(".") + 1);
            value = value.substring(value.indexOf("."), value.lastIndexOf("."));
            methodName = value.substring(value.lastIndexOf(".") + 1);
            value = value.substring(value.indexOf(".") + 1, value.lastIndexOf("."));
        } catch (IndexOutOfBoundsException e) {
            logger.error("检查表达式是否正确");
            e.printStackTrace();
        }
        ExecuteClassParticulars particulars = new ExecuteClassParticulars();
        AccessModifier accessModifier = null;
        switch (qualifier) {
            case "public":
                accessModifier = AccessModifier.Public;
                break;
            case "private":
                accessModifier = AccessModifier.Private;
                break;
            case "protected":
                accessModifier = AccessModifier.Protected;
                break;
            case "*":
                accessModifier = AccessModifier.All;
                break;
        }
//      System.out.println("方法名称"+methodName);
//      System.out.println("方法返回值类型"+returnType);
//      System.out.println("访问修饰符"+qualifier);
//      System.out.println("包名或类名"+value);
        particulars.setAccessModifier(accessModifier);
        particulars.setPagePath(value);
        particulars.setReturnType(returnType);
        particulars.setMethodName(methodName);
        return particulars;
    }

    /**
     * 判断一个类全路径是否包含在表达式里面
     *
     * @param
     */
    public static boolean isExist(String path, String value) {
        path = splitPath(path);
        String[] split = path.split("\\.");
        String[] split1 = value.split("\\.");

        if (path.contains("*")) {
            for (int i = 0; i < split.length - 2; i++) {
                if (!split[i].equals(split1[i])) {
                    return false;
                }
            }
        } else {
            if (split.length < split1.length)
                return false;
            for (int i = 0; i < split.length - 1; i++) {
                if (!split[i].equals(split1[i])) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 解析aop,返回类或包路径
     *
     * @param s
     * @return
     */
    public static String splitPath(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = s.split("\\.");
        for (int i = 1; i < split.length - 2; i++) {
            stringBuilder.append(split[i]);
            if (i != split.length - 3)
                stringBuilder.append(".");
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        AopResult aopResult = new AopResult();
        aopResult.setValue("public.com.jame.*.*.*");
        analysis(aopResult);
    }

}
